<?php
/**
 *      ┏┛ ┻━━━━━┛ ┻┓
 *      ┃　　　　　　 ┃
 *      ┃　　　━　　　┃
 *      ┃　┳┛　  ┗┳　┃
 *      ┃　　　　　　 ┃
 *      ┃　　　┻　　　┃
 *      ┃　　　　　　 ┃
 *      ┗━┓　　　┏━━━┛
 *        ┃　　　┃   神兽保佑,代码无BUG！
 *        ┃　　　┃   @author XingZi
 *        ┃　　　┗━━━━━━━━━┓
 *        ┃　　　　　　　    ┣┓
 *        ┃　　　　         ┏┛
 *        ┗━┓ ┓ ┏━━━┳ ┓ ┏━┛
 *          ┃ ┫ ┫   ┃ ┫ ┫
 *          ┗━┻━┛   ┗━┻━┛
 */
namespace app\admin\controller[NAMESPACE];

use think\Db;
use think\Loader;
use think\Config;

class [NAME] extends Admin
{
    public function index()
    {
        return $this->view->fetch();
    }

    public function indexAjax()
    {
        //查询条件
        $where = [];
        [WHERE]
        //排序字段
        $order = $this->request->param('_order') ?: '[PK]';
        $sort = null !== $this->request->param('_sort') ? ($this->request->param('_sort') == 'asc' ? 'asc' : 'desc') : (false ? 'asc' : 'desc');
        $order_by = $order ? "{$order} {$sort}" : false;

        //查询分页数据
        $nowPage = $this->request->has('page', 'param', true) ? $this->request->param('page') : 1;
        $res = Db::name('[NAME]')->where($where)->order($order_by)->paginate(null, false, ['page' => $nowPage]);
        $res_arr = $res->toArray();
        $page = $res->render();
        $count = $res_arr['total']; //计算总页数
        $list = $res_arr['data'];
        $allPage = intval(ceil($count / Config::get('paginate.list_rows')));

        $this->view->assign('nowPage', $nowPage); //当前页
        $this->view->assign('allPage', $allPage); //总页数
        $this->view->assign('count', $count);
        $this->view->assign('page', $page);
        $this->view->assign('list', $list);

        return ajax_return('', $this->view->fetch());
    }

    public function add()
    {
        if ($this->request->isAjax()) {

            $data = $this->request->param();
            unset($data['id']);

            [DATE]

            // 验证
            $validate = Loader::validate('[NAME]');
            if (!$validate->scene('add')->check($data)) {
                return ajax_return_error($validate->getError());
            }

            $res = Db::name('[NAME]')->insert($data);
            if ($res > 0) {
                return ajax_return('添加成功');
            } else {
                return ajax_return_error('添加失败');
            }

        } else {
            return $this->view->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isAjax()) {

            $data = $this->request->param();
            if (!$data['id']) {
                return ajax_return_error('缺少参数ID');
            } else {
                $id = $data['id'];
                unset($data['id']);
            }

            // 验证
            $validate = Loader::validate('[NAME]');
            if (!$validate->scene('edit')->check($data)) {
                return ajax_return_error($validate->getError());
            }

            $res = Db::name('[NAME]')->where(['[PK]' => $id])->update($data);
            if ($res > 0) {
                return ajax_return('编辑成功');
            } else {
                return ajax_return_error('编辑失败');
            }
        } else {
            $id = $this->request->param('id');
            $li = Db::name('[NAME]')->where(['[PK]' => $id])->find();

            $this->view->assign('id', $id);
            $this->view->assign('li', $li);

            return $this->view->fetch();
        }
    }

    public function delete()
    {
        $ids = $this->request->param('id');
        $where['[PK]'] = ['in', $ids];
        $res = Db::name('[NAME]')->where($where)->delete();
        if ($res > 0) {
            return ajax_return('删除成功');
        } else {
            return ajax_return_error('删除失败');
        }
    }

}